//dep
window.ServiceUtil = new function(){

	var inc = 0;
	var gensym;
	
	this.init = function(k){
		if (!gensym) 
			ServerProxy.gensym(function(ret){
				gensym = ret;
				k();
			});
		else 
			k();
	}

	
	this.generateID = function(seed){
		return "__titt__" + seed + "__" + this.generateToken();
	}
	
	this.generateToken = function() {
		return gensym + "x" + inc++;
	}
	
	this.generateDomain = function() {
		return "http://" + this.generateToken() + ".titt." + location.host;
	}
	
	this.generateListenerKey = function() {
		return this.generateInternalKey("listener");
	}
	
	this.generateRegistrationKey = function() {
		return this.generateInternalKey("registration");
	}
	
	this.generateInternalKey = function(cat) {
		return ["internal", cat, this.generateToken()];
	}
	
	
	this.setupForm = function(form, action, useDomain) {
		
		form.method = "POST";

		form.enctype = "multipart/form-data";
		form.encoding = "multipart/form-data";
		
		if(useDomain)
			form.action = this.generateDomain() + action;
		else
			form.action = action;
		
		if (form.getElementsByTagName) {
			var inps = form.getElementsByTagName("input");
			
			for (var i = 0; i < inps.length; i++) 
				if (!inps[i].name) 
					inps[i].name = this.generateID("nuderrequest_input");
		}
	}
	
	this.createTargets = function(form) {
		var id = this.generateID("nuderrequest_frame");

		var d = document.createElement("div");
		d.style.display = "none";
		d.innerHTML = "<iframe name='"+id+"' id='"+id+"'></iframe>";

		document.body.appendChild(d);
		
		form.target = id;
		
		return [d, d.getElementsByTagName("iframe")[0]];
	}
	
	this.cleanUp = function(targets, form) {
		if(form != null && form.abort)
			form.abort(form.target);

		document.body.removeChild(targets[0]);
	}	
	
	this.createListener = function(list, end) {
		var l = new function() {
			
			this.onInitiate = function(total) {
				if(list)
					list.onInitiate(total);
			}
			this.onConnectionUpdate = function(read, total) {
				if(list)
					list.onConnectionUpdate(read, total);
			} 
			this.onComplete = function(total) {
				if(list)
					list.onComplete(total);
				doEnd(true);
			}
			this.onError = function() {
				if(list)
					list.onError();
				doEnd(false);
			}
			
			function doEnd(suc) {
				if (end)
					end(suc);
				end = null;
			}
		};
		return new IOTimeoutListener(l);
	}

	/*
	 	this.createListener = function(list, end, target) {
		var l = new function() {
			var comp = false;

			if(target)
				target.onload = function() {
					target.onload = null;
					if(comp) 
						doEnd(true);
					else 
						comp = true;
				}
			else
				comp = true;
			
			this.onInitiate = function(total) {
				if(list)
					list.onInitiate(total);
			}
			this.onConnectionUpdate = function(read, total) {
				if(list)
					list.onConnectionUpdate(read, total);
			} 
			this.onComplete = function(total) {
				if(list)
					list.onComplete(total);
alert("gogo end " + comp)
				if(comp)
					doEnd(true);
				else
					comp = true;
			}
			this.onError = function() {
				if(list)
					list.onError();
				doEnd(false);
			}
			
			function doEnd(suc) {
				alert("doEnd")
				if (end)
					end(suc);
				end = null;
			}
		};
		return new IOTimeoutListener(l);
	}

	 */	
}
